package com.pignest.hotel.module.global;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.hotel.beans.Member;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;
import com.pignest.hotel.util.UiUtil;

@Deprecated
public class MemberWindow extends Window {
	private static final long serialVersionUID = 2147239767923279156L;
	private String id;
	private Member member;
	
	private Textbox nameField;//会员姓名
	private Listbox typeListbox;//类型 如个人、公司、中介/旅行社
	private Listbox sexListbox;//性别
	
	private Textbox mobileField;//手机
	private Listbox certificateTypeListbox;//证件类型
	private Textbox certificateNoField;//证件号
	
	private Textbox mailField;//邮件
	
	private Listbox registerModeListbox;//注册方式，现金购买、赠送
	private Listbox sendCardWayListbox;//发卡渠道,购买
	
	private Listbox nationListbox;//国籍;
	private Listbox provinceListbox;//省份
	private Textbox streetField;//街道;
	
	private Textbox postField;//邮编;
	
	private Textbox cardNoField;//卡号
	private Textbox cardSerial;//卡序列号
	
	private Listbox memberLevelListbox;//会员级别如金卡、银卡、普通
	

	private Datebox birthdayField;
	
	private void init(){
		
		nameField = (Textbox)getFellow("nameField");
		typeListbox=(Listbox)getFellow("typeListbox");
		sexListbox=(Listbox)getFellow("sexListbox");
		mobileField = (Textbox)getFellow("mobileField");
		certificateTypeListbox=(Listbox)getFellow("certificateTypeListbox");
		certificateNoField = (Textbox)getFellow("certificateNoField");
		mailField = (Textbox)getFellow("mailField");
		registerModeListbox=(Listbox)getFellow("registerModeListbox");
		sendCardWayListbox=(Listbox)getFellow("sendCardWayListbox");
		nationListbox=(Listbox)getFellow("nationListbox");
		provinceListbox=(Listbox)getFellow("provinceListbox");
		streetField = (Textbox)getFellow("streetField");
		postField = (Textbox)getFellow("postField");
		
		cardNoField = (Textbox)getFellow("cardNoField");
		cardSerial =(Textbox)getFellow("cardSerial");
		memberLevelListbox=(Listbox)getFellow("memberLevelListbox");

		
		birthdayField = (Datebox)getFellow("birthdayField");
		
	
	}
	private void setMember(Member member){
		init();
	 nameField.setValue(member.getName());
	 nameField.setReadonly(true);
	 UiUtil.setListboxSelect(sexListbox,member.getSex());
	 UiUtil.setListboxSelect(typeListbox,member.getType());
	 mobileField.setValue(member.getMobile());
	 UIUtils.setSelectedItem(certificateTypeListbox,member.getCertificatType());
	// UiUtil.setListboxSelect(certificateTypeListbox,member.getCertificatType());
	 certificateNoField.setValue(member.getCertificateNo());
	 mailField.setValue(member.getMail());
	 UiUtil.setListboxSelect(registerModeListbox,member.getRegisterMode());
	 UiUtil.setListboxSelect(sendCardWayListbox,member.getSendCardWay());
	 UiUtil.setListboxSelect(nationListbox,member.getNation());
	 UiUtil.setListboxSelect(provinceListbox,member.getProvince());
	 streetField.setValue(member.getStreet());
	 postField.setValue(member.getPost());
	 //UiUtil.setListboxSelect(memberLevelListbox,member.getMemberLevel());
	 cardNoField.setValue(member.getCardNo());

	// birthdayField.setValue(user.getBirthday());
	
	
	 
	 
 }
 public boolean save(){
	 
	 init();
	 String name = nameField.getValue();
	 String cardSerialno =cardSerial.getValue();
	 if (StringUtils.isEmpty(name)) {
		return false;
	 }
	 MemberService server =Services.getMemberService();
	 Member member = new Member();
	 member.setName(name);
	// member.setCardNo(cardNoField.getValue());
	 String sex=(String)sexListbox.getSelectedItem().getAttribute("value");
     member.setSex(sex);
     member.setMobile(mobileField.getValue());
     //member.setCertificatType((String)certificateTypeListbox.getSelectedItem().getAttribute("value"));
     member.setCertificatType((String)UIUtils.getSelectedValue(certificateTypeListbox));
     member.setCertificateNo(certificateNoField.getValue());
     member.setType((String)typeListbox.getSelectedItem().getAttribute("value"));
     member.setMail(mailField.getValue());
     member.setRegisterMode((String)registerModeListbox.getSelectedItem().getAttribute("value"));
     member.setSendCardWay((String)sendCardWayListbox.getSelectedItem().getAttribute("value"));
     member.setNation((String)nationListbox.getSelectedItem().getAttribute("value"));
     member.setProvince((String)provinceListbox.getSelectedItem().getAttribute("value"));
     member.setStreet(streetField.getValue());
     member.setPost(postField.getValue());
    // member.setMemberLevel((String)memberLevelListbox.getSelectedItem().getAttribute("value"));
     //member.setState("在用");//进行发卡后才设置状态
	 if (StringUtils.isEmpty(id)) {//新增
		/* if (server.exists(workNo)){
		     return false;
	     }*/
		 server.addMember(member,cardSerialno);
	 }else{
		 member.setId(id);
		 server.updateMember(member,cardSerialno);
	 }
	 return true;
 }
 public void setMemberId(String id){
	 
 try{
	 if(id!=null){
		 Member member =Services.getMemberService().getMember(id);
	 if(member!=null){
		 this.id=id;
		 this.member=member;
	  }
	 }
	 setMember(member);
	 }catch(Exception e){
		 e.printStackTrace();
		 
	 }
 }


}
